package com.siemens.industrial.web.mvc.service.partner;

import com.siemens.industrial.web.mvc.dao.partner.PartnerPlanDowntimeDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by shuan on 2017/1/19.
 */
@Service
public class PartnerPlanDowntimeService {

    @Resource
    PartnerPlanDowntimeDao partnerPlanDowntimeDao;

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public List<Map<String,Object>> getDevicePlanDowntime(String tenantId, String deviceId){
        return partnerPlanDowntimeDao.findDevicePlanDowntimes(tenantId,deviceId);
    }

    public void addPlanDowntime(String tenantId,String deviceId,String startTime,String endTime) throws ParseException {
        Date beginDate = simpleDateFormat.parse(startTime);
        Date endDate = simpleDateFormat.parse(endTime);
        List<Map<String,Object>> intersections = partnerPlanDowntimeDao.findIntersections(tenantId,deviceId,startTime,endTime);
        if (intersections.size() == 0){
            this.insert(tenantId,deviceId,beginDate,endDate);
        }else {
            Date firstBeginTime = simpleDateFormat.parse(intersections.get(0).get("start_time").toString());
            Date lastEndTime = simpleDateFormat.parse(intersections.get(intersections.size() - 1).get("end_time").toString());
            if ((beginDate.before(firstBeginTime) || beginDate.equals(firstBeginTime)) && (endDate.before(lastEndTime) || endDate.equals(lastEndTime) || endDate.equals(firstBeginTime))) {
                this.deleteAll(tenantId,deviceId,intersections);
                this.insert(tenantId,deviceId,beginDate,lastEndTime);
            } else if ((beginDate.before(firstBeginTime) || beginDate.equals(firstBeginTime)) && (endDate.after(lastEndTime))) {
                this.deleteAll(tenantId,deviceId,intersections);
                this.insert(tenantId,deviceId,beginDate,endDate);
            } else if ((beginDate.after(firstBeginTime)) && (endDate.before(lastEndTime) || endDate.equals(lastEndTime))) {
                this.deleteAll(tenantId,deviceId,intersections);
                this.insert(tenantId,deviceId,firstBeginTime,lastEndTime);
            }else if((beginDate.after(firstBeginTime) || beginDate.equals(lastEndTime)) && endDate.after(lastEndTime)){
                this.deleteAll(tenantId,deviceId,intersections);
                this.insert(tenantId,deviceId,firstBeginTime,endDate);
            }
        }
    }

    public void deletePlanDowntime(String tenantId,String deviceId,String startTime,String endTime){
        partnerPlanDowntimeDao.deletePlanDowntime(tenantId,deviceId,startTime,endTime);
    }

    public void insert(String tenantId,String deviceId,Date startTime,Date endTime){
        partnerPlanDowntimeDao.savePlanDowntime(tenantId,deviceId,startTime,endTime);
    }

    public void deleteAll(String tenantId,String deviceId,List<Map<String,Object>> intersections){
        for (int i = 0;i<intersections.size();i++){
            deletePlanDowntime(tenantId, deviceId, intersections.get(i).get("start_time").toString(),intersections.get(i).get("end_time").toString());
        }
    }
}
